package  
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import utils.Direction;
	import utils.MathHelper;
	/**
	 * ...
	 * @author Ivan
	 */
	public class Camera 
	{
		//源点坐标
		public var location:Point;
		
		//视口
		private var _viewPort:Rectangle;
		
		//location的有效区域
		private var _validArea:Rectangle = new Rectangle();
		
		//主角坐标
		public var partyLeaderPosition:Point;
		public var partyLeaderScreenPosition:Point = new Point();
		
		public var squareAcross:int;
		public var squareDown:int;
		
		private var _dist:int;
		private var _speedX:Number = 2;
		private var _speedY:Number = 1.5;
		private var _dirIns:Point;
		
		//地图块大小
		private var _cellWidth:int = 0;
		private var _cellHeight:int = 0;
		public function get cellWidth():int { return _cellWidth; }
		public function get cellHeight():int { return _cellHeight;}
		
		
		//地图块宽高
		private var _cellsAcross:int;
		private var _cellsDown:int;
		public function get cellsAcross():int { return _cellsAcross; }
		public function get cellsDown():int { return _cellsDown;}
		
		private var _cellPosition:Point = new Point(0, 0);
		
	
		public function get cellPosition():Point
		{
			return _cellPosition;
		}
		
		
		public function set viewPort(port:Rectangle):void 
		{
			_viewPort = port;
			_cellWidth = port.width / 2;
			_cellHeight = port.height / 2;
		}
		
		/**
		 * 设置/获取当前地图
		 */
		private var _map:Map;
		public function get map():Map { return _map; }
		public function set map(value:Map):void 
		{
			_map = value;
			
			//设置有效区域
			_validArea.width = map.mapWidth * map.tileWidth - _viewPort.width / 2;
			_validArea.height = map.mapHeight * map.tileHeight - _viewPort.height / 2;
			
			//根据地图出生点设置主角位置
			partyLeaderPosition.x = _map.spawnPosition.x * map.tileWidth;
			partyLeaderPosition.y = _map.spawnPosition.y * map.tileHeight;
			
			
			//计算场景tile宽高
			squareAcross = _viewPort.width / _map.tileWidth;
			squareDown = _viewPort.height / _map.tileHeight;
			
			//计算cell size;
			_cellsAcross = _map.mapWidth * _map.tileWidth / _viewPort.width * 2;
			_cellsDown = _map.mapHeight * _map.tileHeight / _viewPort.height * 2;
			
			
			updateLocation();
		}
		
		//是否在移动
		public var isMoving:Boolean = false;
		
		public function Camera() 
		{
			location = new Point();
			partyLeaderPosition = new Point();
		}
		
		/**
		 * 创建一个移动补间
		 * @param	dir
		 */
		public function creatMovement(dir:int):void 
		{
			
			isMoving = true;
			_dist = _map.tileWidth / _speedX;
			
			_dirIns = Direction.getIncrement(dir);
		}
		
		/**
		 * 更新地图与主角坐标
		 */
		private function updateLocation():void 
		{
			location.x = MathHelper.Clamp(partyLeaderPosition.x - _viewPort.width / 2, 0, _validArea.width - _viewPort.width/2);
			location.y = MathHelper.Clamp(partyLeaderPosition.y - _viewPort.height / 2, 0, _validArea.height-_viewPort.height/2);
			
			if (partyLeaderPosition.x < _viewPort.width / 2)
			{
				partyLeaderScreenPosition.x = partyLeaderPosition.x;
			}
			else if (partyLeaderPosition.x > _validArea.width)
			{
				
				partyLeaderScreenPosition.x = partyLeaderPosition.x - _validArea.width + _viewPort.width / 2;
			}
			else 
			{
				partyLeaderScreenPosition.x = _viewPort.width / 2;
			}
			
			if (partyLeaderPosition.y < _viewPort.height / 2)
			{
				partyLeaderScreenPosition.y = partyLeaderPosition.y;
			}
			else if (partyLeaderPosition.y > _validArea.height)
			{
				partyLeaderScreenPosition.y = partyLeaderPosition.y - _validArea.height + _viewPort.height / 2;
			}
			else 
			{
				partyLeaderScreenPosition.y = _viewPort.height / 2;
			}
			
			_cellPosition.x = int(location.x / _cellWidth);
			_cellPosition.y = int(location.y / _cellHeight);
		}
		
		public function update():void 
		{
			partyLeaderPosition.x += _dirIns.x * _speedX;
			partyLeaderPosition.y += _dirIns.y * _speedY;
			
			
			updateLocation();
			
			_dist--;
			if (_dist == 0)
				isMoving = false;
		}
		
	}

}